﻿Imports System.Data.SqlClient

Public Class Product
    Dim cmd As New SqlCommand
    Public Sub refreshdata()
        grdproduct.Rows.Clear()
        cmd.Connection = cn
        cmd.CommandText = "select ProID,Procode ,ProName ,catName, PriceIN ,PriceOut ,Qty ,Madedate ,Expiredate ,tblproduct.Description  from tblProduct inner join tblCategory on tblProduct .CatID =tblCategory .CatID where ProName like N'%" & txtsearch.Text & "%' or ProID like N'%" & txtsearch.Text & "%' or Procode like N'%" & txtsearch.Text & "%' "
        Dim rd As SqlDataReader = cmd.ExecuteReader
        While rd.Read = True
            Me.grdproduct.Rows.Add(rd(0), rd(1), rd(2), rd(3), rd(4), rd(5), rd(6), rd(7), rd(8), rd(9))
        End While
        rd.Close()
        radname.Checked = True
    End Sub

    Private Sub Product_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        refreshdata()
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim nfrm As New productEditor
        nfrm.btnupdate.Enabled = False
        If nfrm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            refreshdata()
        End If

    End Sub

    Private Sub btndelet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelet.Click
        If Me.grdproduct.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        If MessageBox.Show("Are you want to delete", "Delete Confirme", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        For Each row In grdproduct.SelectedRows
            Dim id = row.Cells("Proid").Value
            cmd.Connection = cn
            cmd.CommandText = "delete from tblproduct where proID= " & id
            cmd.ExecuteNonQuery()
        Next
        refreshdata()
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        refreshdata()
    End Sub

    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        If Me.grdproduct.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row As DataGridViewRow = Me.grdproduct.SelectedRows(0)
        Dim frm As New productEditor
        frm.btnadd.Enabled = False
        frm.txtid.Text = row.Cells("proid").Value
        frm.txtprocode.Text = row.Cells("procode").Value
        frm.txtproname.Text = row.Cells("proname").Value
        frm.txtcatname.Text = row.Cells("catname").Value
        frm.txtpricein.Text = row.Cells("pricein").Value
        frm.txtpricout.Text = row.Cells("priceout").Value
        frm.txtbydate.Text = row.Cells("Made").Value
        frm.txtexpiredate.Text = row.Cells("expire").Value
        frm.txtqty.Text = row.Cells("qty").Value
        frm.txtdescription.Text = row.Cells("discription").Value
        frm.ShowDialog()
        refreshdata()
    End Sub
End Class
